// $codepro.audit.disable
/**
 * License under GNU's GPL 
 * PatientGui.java 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import java.util.Scanner;
import java.io.*;

/**
 * The GUI for the Patient class
 * 
 * @author Team 9
 * @author Paul "Big Z" Zaitsev
 * @version 1.0
 * 
 */
public class PatientGui extends JTabbedPane {
	private JFrame frame;
	
	public JTabbedPane tp;
	
	public JLabel stuff;
	
	private Patient current;
	
	private JTextField textField;
	
	private JTextField textField_1;
	
	private JTextField textField_2;
	
	private JTextField textField_3;
	
	private JLabel lblTreat, lblDate;
	
	private JTextPane textPane_1, textPane, textPane_2;
	
	private JCheckBox checkBox_1, checkBox_2, checkBox_3, checkBox_4, checkBox_5,
			checkBox_6, checkBox_7, checkBox_8, checkBox_9, checkBox;

	/**
	 * Constructor for the Patient GUI
	 * 
	 * @param pat
	 *            the patient being looked at
	 */
	public PatientGui(Patient pat) {
		current = pat;
		initialize();
	}

	/**
	 * the default constructor for patient GUI
	 */
	public PatientGui() {
		initialize();
	}

	/**
	 * the initialize method that the constructor calls
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(800, 600));

		// 4 main tabs
		JPanel history = new JPanel();
		JPanel appointment = new JPanel();
		JPanel logout = new JPanel();
		JPanel main = new JPanel();
		JPanel invoice = new JPanel();
		JPanel chart = new JPanel();
		// ------------------------

		// Everything that appears on main tab
		main.setLayout(new GridLayout(3, 3));
		// JLabel stuff = new JLabel();
		// history.add(stuff);
		// -------------------------

		// Everything that appears on Appointment tab
		// -------------------------

		// Everything that appears on logout tab
		JLabel signoff = new JLabel("Are you sure?");
		JButton yes = new JButton("Yes");
		yes.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				Gui obj = new Gui();
				obj.getFrame().setVisible(true);
				setVisible(false);
				frame.setVisible(false);
				removeAll();
			}
		});
		JButton no = new JButton("No");
		no.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				setSelectedIndex(0);
			}
		});
		logout.add(signoff);
		logout.add(yes);
		logout.add(no);
		// -------------------------

		addTab("Main", main);

		JPanel panel = new JPanel();
		main.add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("Welcome "); // +
														// current.getFirstName()+
														// "!");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(320, 11, 161, 24);
		panel.add(lblNewLabel);
		addTab("History", history);
		history.setLayout(null);

		lblTreat = new JLabel("Treatment Records for Appointment on: ");
		lblTreat.setBounds(46, 17, 400, 14);
		history.add(lblTreat);

		lblDate = new JLabel(" ");
		lblDate.setBounds(300, 17, 100, 14);
		history.add(lblDate);

		JLabel lblNewLabel_4 = new JLabel("Doctor's Orders-");
		lblNewLabel_4.setBounds(46, 263, 124, 14);
		history.add(lblNewLabel_4);

		JLabel label = new JLabel("BP:");
		label.setBounds(46, 53, 46, 14);
		history.add(label);

		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		textField.setBounds(96, 50, 86, 20);
		history.add(textField);

		JLabel label_1 = new JLabel("Weight:");
		label_1.setBounds(203, 53, 50, 14);
		history.add(label_1);

		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setColumns(10);
		textField_1.setBounds(281, 50, 86, 20);
		history.add(textField_1);

		JLabel label_2 = new JLabel("Height:");
		label_2.setBounds(46, 76, 46, 14);
		history.add(label_2);

		textField_2 = new JTextField();
		textField_2.setEditable(false);
		textField_2.setColumns(10);
		textField_2.setBounds(96, 78, 86, 20);
		history.add(textField_2);

		textField_3 = new JTextField();
		textField_3.setEditable(false);
		textField_3.setColumns(10);
		textField_3.setBounds(281, 73, 86, 20);
		history.add(textField_3);

		JLabel label_3 = new JLabel("Tempurature:");
		label_3.setBounds(192, 76, 90, 14);
		history.add(label_3);

		JLabel label_4 = new JLabel("Chief Complaints:");
		label_4.setBounds(46, 109, 136, 14);
		history.add(label_4);

		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setBounds(46, 129, 383, 40);
		history.add(textPane);

		JLabel label_5 = new JLabel("Diagnosis:");
		label_5.setBounds(46, 180, 89, 14);
		history.add(label_5);

		textPane_1 = new JTextPane();
		textPane_1.setEditable(false);
		textPane_1.setBounds(46, 200, 383, 40);
		history.add(textPane_1);

		JLabel label_6 = new JLabel("Lab Work:");
		label_6.setBounds(46, 288, 59, 14);
		history.add(label_6);

		checkBox = new JCheckBox("Blood Test");
		checkBox.setBounds(128, 284, 86, 23);
		history.add(checkBox);

		checkBox_1 = new JCheckBox("X-Ray");
		checkBox_1.setBounds(128, 313, 86, 23);
		history.add(checkBox_1);

		checkBox_2 = new JCheckBox("MRI");
		checkBox_2.setBounds(216, 313, 97, 23);
		history.add(checkBox_2);

		checkBox_3 = new JCheckBox("Throat Culture");
		checkBox_3.setBounds(216, 284, 97, 23);
		history.add(checkBox_3);

		checkBox_4 = new JCheckBox("Lumbar Puncture");
		checkBox_4.setBounds(315, 284, 114, 23);
		history.add(checkBox_4);

		JLabel label_7 = new JLabel("Prescriptions:");
		label_7.setBounds(46, 343, 76, 14);
		history.add(label_7);

		checkBox_5 = new JCheckBox("Vicodin");
		checkBox_5.setBounds(128, 341, 86, 23);
		history.add(checkBox_5);

		checkBox_6 = new JCheckBox("Zyrtec");
		checkBox_6.setBounds(216, 339, 97, 23);
		history.add(checkBox_6);

		checkBox_7 = new JCheckBox("Prozac");
		checkBox_7.setBounds(315, 339, 97, 23);
		history.add(checkBox_7);

		checkBox_8 = new JCheckBox("Ibuprofen");
		checkBox_8.setBounds(128, 367, 86, 23);
		history.add(checkBox_8);

		checkBox_9 = new JCheckBox("Keflex");
		checkBox_9.setBounds(216, 367, 97, 23);
		history.add(checkBox_9);

		JLabel label_8 = new JLabel("Follow Up Instructions");
		label_8.setBounds(46, 407, 136, 14);
		history.add(label_8);

		textPane_2 = new JTextPane();
		textPane_2.setEditable(false);
		textPane_2.setText(" ");
		textPane_2.setBounds(46, 423, 383, 40);
		history.add(textPane_2);
		addTab("Appointment", appointment);
		appointment.setLayout(null);

		JLabel lblAppointmentDetails = new JLabel(
				"Please Enter Appointment Details");
		lblAppointmentDetails.setHorizontalAlignment(SwingConstants.CENTER);
		lblAppointmentDetails.setBounds(302, 11, 250, 20);
		appointment.add(lblAppointmentDetails);

		JLabel lblNewLabel_1 = new JLabel("Date Desired:");
		lblNewLabel_1.setBounds(175, 62, 100, 20);
		appointment.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("Time Desired:");
		lblNewLabel_2.setBounds(175, 110, 100, 14);
		appointment.add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("Doctor Desired:");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_3.setBounds(175, 158, 109, 14);
		appointment.add(lblNewLabel_3);

		String[] mm = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
				"10", "11", "12" };
		final JComboBox comboBox = new JComboBox(mm);
		comboBox.setBounds(327, 62, 68, 20);
		appointment.add(comboBox);

		String[] dd = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
				"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
				"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
				"30", "31" };
		final JComboBox comboBox_1 = new JComboBox(dd);
		comboBox_1.setBounds(405, 62, 70, 20);
		appointment.add(comboBox_1);

		String[] yy = { "2011", "2012", "2013" };
		final JComboBox comboBox_2 = new JComboBox(yy);
		comboBox_2.setBounds(485, 62, 86, 20);
		appointment.add(comboBox_2);

		String[] hh = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
				"10", "11", "12" };
		final JComboBox comboBox_3 = new JComboBox(hh);
		comboBox_3.setBounds(327, 107, 68, 20);
		appointment.add(comboBox_3);

		String[] min = { "00", "15", "30", "45" };
		final JComboBox comboBox_4 = new JComboBox(min);
		comboBox_4.setBounds(405, 107, 76, 20);
		appointment.add(comboBox_4);

		String[] ap = { "AM", "PM" };
		final JComboBox comboBox_5 = new JComboBox(ap);
		comboBox_5.setBounds(485, 107, 75, 20);
		appointment.add(comboBox_5);

		int arrSize = Gui.getDoctorArray().size();
		String[] doctors = new String[arrSize];
		for (int i = 0; i < arrSize; i++) {
			doctors[i] = Gui.getDoctorArray().get(i).getFirstName();
		}
		final JComboBox comboBox_6 = new JComboBox(doctors);
		comboBox_6.setBounds(327, 155, 148, 20);
		appointment.add(comboBox_6);

		final JLabel lblNewLabel_5 = new JLabel("Appointment Made!");
		lblNewLabel_5.setVisible(false);
		lblNewLabel_5.setBounds(327, 251, 148, 23);
		appointment.add(lblNewLabel_5);

		final JLabel lblDateUnavailableTry = new JLabel(
				"Date unavailable, try again!");
		lblDateUnavailableTry.setVisible(false);
		lblDateUnavailableTry.setBounds(327, 285, 202, 23);
		appointment.add(lblDateUnavailableTry);

		Scanner fileScan = null; // first and last name of the field selected

		try {
			// fileScan = new Scanner(new File("Patient/Rogers, April.txt")); //
			// +
			fileScan = new Scanner(new File("Patient/" + current.getLastName()
					+ ", " + current.getFirstName() + ".txt"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JCheckBox[] checkBoxes = { checkBox, checkBox_3, checkBox_4,
				checkBox_1, checkBox_2, checkBox_5, checkBox_6, checkBox_7,
				checkBox_8, checkBox_9 };

		String he = fileScan.nextLine();
		if (he.equals("Treatment Record for Appointment on: ")) {
			lblTreat.setText("No Appointment on Record!");
		} else {
			lblTreat.setText(he);
			fileScan.nextLine();
			fileScan.nextLine();
			// fileScan.nextLine();
			// lblDoctor.setText(fileScan.nextLine());
			// lblNurse.setText(fileScan.nextLine());
			textField.setText(fileScan.nextLine());
			textField_2.setText(fileScan.nextLine());
			textField_1.setText(fileScan.nextLine());
			textField_3.setText(fileScan.nextLine());
			textPane.setText(fileScan.nextLine());
			textPane_1.setText(fileScan.nextLine());
			textPane_2.setText(fileScan.nextLine());

			for (int i = 0; i < checkBoxes.length; i++) {
				checkBoxes[i].setEnabled(false);
				checkBoxes[i].setSelected(fileScan.nextBoolean());
			}
		}

		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String mm = (String) comboBox.getSelectedItem();
				String dd = (String) comboBox_1.getSelectedItem();
				String yy = (String) comboBox_2.getSelectedItem();
				String hh = (String) comboBox_3.getSelectedItem();
				String min = (String) comboBox_4.getSelectedItem();
				String ap = (String) comboBox_5.getSelectedItem();
				String doc = (String) comboBox_6.getSelectedItem();
				boolean check = appointCheck(mm, dd, yy, hh, min, ap, doc);
				if (check) {
					// adds appointment to personal file
					try {
						FileWriter fstream = new FileWriter(
								"Patient/Rogers, April.txt", true);
						BufferedWriter out = new BufferedWriter(fstream);
						out.write(mm + "/" + dd + "/" + yy + " \n");
						out.write("Doctor: " + doc + " \n");
						out.write("Nurse: Lilly \n");
						for (int i = 0; i < 7; i++) {
							out.write(" \n");
						}
						for (int i = 0; i < 10; i++) {
							out.write("false \n");
						}
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					try {
						Appointment appoint = new Appointment(mm, dd, yy, hh,
								min, ap, doc);
						Gui.getAppointArray().add(appoint);
						FileWriter fstream = new FileWriter("Appointments.txt",
								true);
						BufferedWriter out = new BufferedWriter(fstream);
						out.write(mm + "/" + dd + "/" + yy + " " + hh + ":"
								+ min + ap + " " + doc + " \n");
						out.close();
					} catch (Exception e) {// Catch exception if any
						System.err.println("Error: " + e.getMessage());
					}

					lblNewLabel_5.setVisible(true);

				} else {
					lblDateUnavailableTry.setVisible(true);
				}
			}
		});
		btnSubmit.setBounds(352, 203, 89, 23);
		appointment.add(btnSubmit);

		addTab("Invoice", invoice);
		invoice.setLayout(null);

		JLabel lblNewLabel_6 = new JLabel("Treatments Received-");
		lblNewLabel_6.setBounds(10, 11, 153, 14);
		invoice.add(lblNewLabel_6);

		JLabel lblNewLabel_7 = new JLabel("Blood Test");
		lblNewLabel_7.setEnabled(checkBoxes[0].isSelected());
		lblNewLabel_7.setBounds(10, 36, 87, 14);
		invoice.add(lblNewLabel_7);

		JLabel lblNewLabel_8 = new JLabel("X-Ray\r\n");
		lblNewLabel_8.setEnabled(checkBoxes[3].isSelected());
		lblNewLabel_8.setBounds(10, 61, 87, 14);
		invoice.add(lblNewLabel_8);

		JLabel lblLumbarPuncture = new JLabel("Lumbar Puncture");
		lblLumbarPuncture.setEnabled(checkBoxes[2].isSelected());
		lblLumbarPuncture.setBounds(10, 137, 117, 14);
		invoice.add(lblLumbarPuncture);

		JLabel lblThroatCulture = new JLabel("Throat Culture");
		lblThroatCulture.setEnabled(checkBoxes[1].isSelected());
		lblThroatCulture.setBounds(10, 112, 87, 14);
		invoice.add(lblThroatCulture);

		JLabel lblMri = new JLabel("MRI");
		lblMri.setEnabled(checkBoxes[4].isSelected());
		lblMri.setBounds(10, 87, 71, 14);
		invoice.add(lblMri);

		JLabel lblProzac = new JLabel("Prozac");
		lblProzac.setEnabled(checkBoxes[5].isSelected());
		lblProzac.setBounds(10, 212, 87, 14);
		invoice.add(lblProzac);

		JLabel lblZyrtec = new JLabel("Zyrtec");
		lblZyrtec.setEnabled(checkBoxes[6].isSelected());
		lblZyrtec.setBounds(10, 187, 117, 14);
		invoice.add(lblZyrtec);

		JLabel lblVicodin = new JLabel("Vicodin");
		lblVicodin.setEnabled(checkBoxes[7].isSelected());
		lblVicodin.setBounds(10, 162, 117, 14);
		invoice.add(lblVicodin);

		JLabel lblNewLabel_9 = new JLabel("Ibuprofen");
		lblNewLabel_9.setEnabled(checkBoxes[8].isSelected());
		lblNewLabel_9.setBounds(10, 237, 87, 14);
		invoice.add(lblNewLabel_9);

		JLabel lblNewLabel_10 = new JLabel("Keflex");
		lblNewLabel_10.setEnabled(checkBoxes[9].isSelected());
		lblNewLabel_10.setBounds(10, 262, 87, 14);
		invoice.add(lblNewLabel_10);

		JLabel lblNewLabel_11 = new JLabel((String) null);
		lblNewLabel_11.setEnabled(false);
		lblNewLabel_11.setBounds(191, 36, 46, 14);
		invoice.add(lblNewLabel_11);

		int cost = 0;
		for (int i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].isSelected()) {
				cost += 50;
			}
		}
		JLabel lblNewLabel_12 = new JLabel("Total cost: $" + cost);
		lblNewLabel_12.setBounds(117, 297, 140, 14);
		invoice.add(lblNewLabel_12);

		genChart(textField.getText(), textField_1.getText(),
				textField_2.getText(), textField_3.getText());
		JLabel img = new JLabel(new ImageIcon("chart.jpg"));
		chart.add(img);
		addTab("Chart", chart);
		addTab("Logout", logout);

		frame.getContentPane().add(this);
		frame.pack();

		// Scanner fileScan = null; // first and last name of the field selected
		//
		// try {
		// fileScan = new Scanner(new File("Patient/Rogers, April.txt")); // +
		// current.getLastName() + ", " + current.getFirstName() + ".txt"));
		// } catch (FileNotFoundException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		//
		// lblTreat.setText(fileScan.nextLine());
		// fileScan.nextLine();
		// fileScan.nextLine();
		// fileScan.nextLine();
		// //lblDoctor.setText(fileScan.nextLine());
		// //lblNurse.setText(fileScan.nextLine());
		// textField.setText(fileScan.nextLine());
		// textField_2.setText(fileScan.nextLine());
		// textField_1.setText(fileScan.nextLine());
		// textField_3.setText(fileScan.nextLine());
		// textPane.setText(fileScan.nextLine());
		// textPane_1.setText(fileScan.nextLine());
		// textPane_2.setText(fileScan.nextLine());
		// JCheckBox[] checkBoxes = {checkBox, checkBox_3, checkBox_4,
		// checkBox_1, checkBox_2, checkBox_5, checkBox_6, checkBox_7,
		// checkBox_8, checkBox_9};
		// for(int i = 0; i < checkBoxes.length; i++){
		// checkBoxes[i].setEnabled(false);
		// checkBoxes[i].setSelected(fileScan.nextBoolean());
		// }

	}

	/**
	 * Returns true if the appointment doesn't exist already
	 * 
	 * @param mm
	 *            month of appointment
	 * @param dd
	 *            day of appointment
	 * @param yy
	 *            year of appointment
	 * @param hh
	 *            hour of appointment
	 * @param min
	 *            minutes of appointment
	 * @param ap
	 *            appointment
	 * @param doc
	 *            doctor requested
	 * @return true/false whether there is already an appointment
	 */
	public boolean appointCheck(String mm, String dd, String yy, String hh,
			String min, String ap, String doc) {
		boolean toReturn = true;
		for (int i = 0; i < Gui.getAppointArray().size(); i++) {
			if (Gui.getAppointArray().get(i).getMm().compareTo(mm) == 0
					&& Gui.getAppointArray().get(i).getDd().compareTo(dd) == 0
					&& Gui.getAppointArray().get(i).getYy().compareTo(yy) == 0
					&& Gui.getAppointArray().get(i).getHh().compareTo(hh) == 0
					&& Gui.getAppointArray().get(i).getMin().compareTo(min) == 0
					&& Gui.getAppointArray().get(i).getAp().compareTo(ap) == 0
					&& Gui.getAppointArray().get(i).getDoctor().compareTo(doc) == 0) {
				toReturn = false;
			}
		}

		return toReturn;
	}

	public void genChart(String bp, String weight, String height, String temp) {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		bp = bp.trim();
		weight = weight.trim();
		height = height.trim();
		temp = temp.trim();
		dataset.setValue(Integer.parseInt(bp), "Value", "BP");
		dataset.setValue(Integer.parseInt(weight), "Value", "Weight");
		// dataset.setValue(Integer.parseInt(height), "Value", "Height");
		dataset.setValue(Integer.parseInt(temp), "Value", "Temperature");
		JFreeChart chart = ChartFactory.createBarChart(
				"Patient Info", "Patient", "Value", dataset,
				PlotOrientation.VERTICAL, false, true, false);
		try {
			ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500,
					300);
		} catch (IOException e) {
			System.err.println("Problem occurred creating chart.");
		}
	}

	public JFrame getFrame() {
		return frame;
	}

	// Main method to construct the Patient panel
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame = new JFrame("Patient");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(800, 600));

		frame.getContentPane().add(new PatientGui());
		frame.pack();
		frame.setVisible(true);
	}
}

/*
 * try{ // Create file FileWriter fstream = new
 * FileWriter("Appointment.txt",true); BufferedWriter out = new
 * BufferedWriter(fstream); out.write(PUT STUFF HERE THAT YOU WANT TO ADD TO THE
 * FILE (probably stuff like dropDownBox.getValue()) " \n"); <-- keep the " \n"
 * that goes to the new line //Close the output stream out.close(); }catch
 * (Exception e){//Catch exception if any System.err.println("Error: " +
 * e.getMessage()); }
 */